//
//  PTLoadingView.m
//  PlanTech
//
//  Created by ruifeng song on 2018/6/19.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "PTLoadingView.h"

@implementation PTLoadingView

+(PTLoadingView *)loadingView
{
    return [[self alloc]init];
}

-(instancetype)init
{
    if (self = [super init]) {
        self.alpha = 1;
        
        PTAnimationIndicator *indicator = [[PTAnimationIndicator alloc]initWithFrame:CGRectMake((self.frame.size.width - 70)/2, (self.frame.size.height - 70)/2, 70, 70)];
//        indicator.imageView.centerX = indicator.centerX + 15;
//        indicator.layer.centerX = ScreenSize.width/2;
        [self addSubview:indicator];
        self.animationIndicator = indicator;
    }
    
    return self;
}

/**
 * 加载并开始动画，默认将视图PTLoadingView 加载在window上.
 */
+(PTLoadingView *)loadingViewAndStartAnimation
{
    PTLoadingView *loadView = [PTLoadingView loadingView];
    loadView.frame = CGRectMake(0, 0, ScreenSize.width, ScreenSize.height);
    
    UIWindow * window = [[[UIApplication sharedApplication]delegate]window];
    loadView.layer.zPosition = INT8_MAX;
    [window addSubview:loadView];
    [loadView.animationIndicator startAnimation];
    return loadView;
}

/**
 * 加载并开始动画，默认将PTLoadingView视图加载在window上.
 *
 * @param frame  frame
 *
 * @param title  显示的标题(加载中...)
 */
+(PTLoadingView *)loadingViewAndStartAnimationWithFrame:(CGRect)frame title:(NSString *)title
{
    PTLoadingView *loadView = [PTLoadingView loadingView];
    if (CGRectIsNull(frame)) {
        frame =CGRectMake(0, 0, ScreenSize.width, ScreenSize.height);
    }
//    [loadView showTitle:title];
    loadView.frame = frame;
    
    //使用keyWindow，当你的app 需要跳转到别的app然后返回本app的时候，有可能会导致UI错乱
    //UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    
    //解决了loadView被VC中view覆盖没法显示的问题（查看视图层级还是被覆盖了）
    loadView.layer.zPosition = INT8_MAX;
    [window addSubview:loadView];
    [loadView.animationIndicator startAnimation];
    return loadView;
}

/**
 *
 * @param  frame  frame
 *
 * @param title 显示的标题 (加载中...)
 *
 * @param view   loadingView 父视图
 */
+(PTLoadingView *)loadingViewAndStartAnimationWithFrame:(CGRect)frame title:(NSString *)title InView:(UIView *)view
{
    PTLoadingView *loadView = [PTLoadingView loadingView];
//    [loadView showTitle:title];
    
    if (CGRectIsNull(frame)) {
        frame = CGRectMake(0, 0, ScreenSize.width, ScreenSize.height);
    }
    loadView.frame = frame;
    [loadView.animationIndicator setFrame:CGRectMake((loadView.frame.size.width - 70)/2, (loadView.frame.size.height - 70)/2, 70, 70)];
    [view addSubview:loadView];
    [loadView.animationIndicator startAnimation];
    return loadView;
}

//此接口可开放出去
-(PTAnimationIndicator *)showTitle:(NSString *)title
{
//    self.animationIndicator.label.text = title;
    return self.animationIndicator;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end















